package com.itfitness.mchart.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.hyphenate.chat.EMClient;
import com.itfitness.mchart.R;
import com.itfitness.mchart.utils.MTextUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * @ProjectName: MChart
 * @Package: com.itfitness.mchart.activity
 * @ClassName: RegistActivity
 * @Description: java类作用描述 ：注册
 * @Author: 作者名：lml
 * @CreateDate: 2019/4/24 11:36
 * @UpdateUser: 更新者：
 * @UpdateDate: 2019/4/24 11:36
 * @UpdateRemark: 更新说明：
 * @Version: 1.0
 */

public class RegistActivity extends BaseActivity {
    @BindView(R.id.activity_regist_et_username)
    EditText activityRegistEtUsername;
    @BindView(R.id.activity_regist_et_password)
    EditText activityRegistEtPassword;
    @BindView(R.id.activity_regist_bt_regist)
    Button activityRegistBtRegist;

    @Override
    protected int LayoutRes() {
        return R.layout.activity_regist;
    }

    @Override
    protected boolean isHaveTitle() {
        return true;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
        initDatas();
    }

    private void initDatas() {
        setTitle("注册", R.color.colorTitleText);
    }

    @OnClick(R.id.activity_regist_bt_regist)
    public void onViewClicked() {
        final String username = MTextUtil.getText(activityRegistEtUsername);
        final String password = MTextUtil.getText(activityRegistEtPassword);
        if(TextUtils.isEmpty(username)){
            ToastUtils.showShort("请输入用户名");
            return;
        }
        if(TextUtils.isEmpty(password)){
            ToastUtils.showShort("请输入密码");
            return;
        }
        Observable.create(new ObservableOnSubscribe<Integer>() {
            @Override
            public void subscribe(ObservableEmitter<Integer> emitter) throws Exception {
                EMClient.getInstance().createAccount(username, password);
                emitter.onNext(1);
            }
        }).subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Integer>() {
                    @Override
                    public void accept(Integer integer) throws Exception {
                        if (integer == 1) {
                            ToastUtils.showShort("注册成功");
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        LogUtils.eTag("注册", throwable.getMessage());
                        ToastUtils.showShort("注册失败");
                    }
                });
    }
}
